//
//  MineMainViewModel.m
//  CarUser
//
//  Created by ayi on 2018/12/24.
//  Copyright © 2018 王森林. All rights reserved.
//

typedef NS_OPTIONS(NSUInteger, TabviewNormalCellType) {
    TabviewNormalCellType_Lab = 0,//
    TabviewNormalCellType_Img = 1,//
    TabviewNormalCellType_Switch = 2,//
    TabviewNormalCellType_Other = 3,//
};

#import "MineMainViewModel.h"

#import "MineMainListTableViewCell.h"
#import "MineTypeTableViewCell.h"
#import "MeHelpViewController.h"
#import "MeAboutViewController.h"
#import "MeSuggestViewController.h"
#import "MeUpViewController.h"
@interface MineMainViewModel()

@property (nonatomic, strong) NSString *count1;
@property (nonatomic, strong) NSString *count2;
@property (nonatomic, strong) NSString *count3;
@property (nonatomic, strong) NSString *count4;

@end

@implementation MineMainViewModel

@synthesize childtab = _childtab;

- (void)setChildtab:(UITableView *)childtab{
    _childtab = childtab;
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.rowHeight = 44;
        self.sectionHeaderHeight = 10;
        self.sectionFooterHeight = .5f;
        self.sectionHeaderColor = UIColor.clearColor;
        
        self.count1 = @"0";
        self.count2 = @"0";
        self.count3 = @"0";
        self.count4 = @"0";
        
        [self resetListAryAndFreshTab];
        
    }
    return self;
}


- (void)resetListAryAndFreshTab{
    self.listAry = @[
                     @[
                         @{@"title":@"列表",@"content":@"",@"imgname":@"",@"type":@"0",@"celltype":@"3"}.mutableCopy
                         ].mutableCopy,
                     
                     @[
                         @{@"title":@"帮助",@"content":@"",@"imgname":@"ic_my_help",@"type":@"1",@"celltype":@"0"}.mutableCopy,
                         @{@"title":@"关于",@"content":@"",@"imgname":@"ic_my_about",@"type":@"2",@"celltype":@"0"}.mutableCopy,
                         @{@"title":@"意见反馈",@"content":@"",@"imgname":@"ic_my_opinion",@"type":@"3",@"celltype":@"0"}.mutableCopy,
                         ].mutableCopy,
                     ].mutableCopy;
    
//    self.count1 = @"1";
//    self.count2 = @"100";
//    self.count3 = @"22";
}




#pragma mark ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.listAry.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *ary = self.listAry[section];
    return ary.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    id model = self.listAry[indexPath.section][indexPath.row];
    
    NSDictionary *modelDic = [model mj_keyValues];
    if ( [modelDic[@"celltype"] integerValue] == TabviewNormalCellType_Other) {
        return 96;
    }
    return 50;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    id model = self.listAry[indexPath.section][indexPath.row];
    
    NSDictionary *modelDic = [model mj_keyValues];
    if ( [modelDic[@"celltype"] integerValue] == TabviewNormalCellType_Other) {
        MineTypeTableViewCell *cell = [MineTypeTableViewCell initWithSteDefaut:tableView andreuseIdentifier:@"MineTypeTableViewCell"];
        
        @weakify(cell);
        [[RACObserve(self, count1) takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id  _Nullable x) {
            @strongify(cell);
            [cell resetCountLabText:TO_STR(x) andindex:0];
        }];
        [[RACObserve(self, count2) takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id  _Nullable x) {
            @strongify(cell);
            [cell resetCountLabText:TO_STR(x) andindex:1];
        }];
        [[RACObserve(self, count3) takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id  _Nullable x) {
            @strongify(cell);
            [cell resetCountLabText:TO_STR(x) andindex:2];
        }];
        [[RACObserve(self, count4) takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id  _Nullable x) {
            @strongify(cell);
            [cell resetCountLabText:TO_STR(x) andindex:3];
        }];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    
    MineMainListTableViewCell *cell;
    if ( [modelDic[@"celltype"] integerValue] == TabviewNormalCellType_Img) {
        cell = [MineMainListTableViewCell initWithStyWithImgleWithTab:tableView andreuseIdentifier:@"MineMainListTableViewCellImg"];
    }else if ( [modelDic[@"celltype"] integerValue] == TabviewNormalCellType_Switch) {
        cell = [MineMainListTableViewCell initWithStyleWithSwitchWithTab:tableView andreuseIdentifier:@"MineMainListTableViewCellSwitch"];
    }else{
        cell = [MineMainListTableViewCell initWithStyleWithTab:tableView andreuseIdentifier:@"MineMainListTableViewCell"];
    }
    cell.leftimgv.image = IMG(model[@"imgname"]);
    cell.textLab.text = model[@"title"];
    cell.detailLab.attributedText = [[NSMutableAttributedString alloc]initWithString:model[@"content"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == ((NSArray *)self.listAry[indexPath.section]).count - 1) {
        cell.underlineView.hidden = YES;
    }else{
        cell.underlineView.hidden = NO;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSLog(@"index%ld",indexPath.row);
    
//    UIViewController *vc = [self viewController];
//    if ([vc respondsToSelector:NSSelectorFromString(@"pushToOrderListWithStauts:")]) {
//        SEL functionSelector = NSSelectorFromString(@"pushToOrderListWithStauts:");
//        NSMethodSignature *signature = [vc methodSignatureForSelector:functionSelector];
//        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
//        [invocation setTarget:vc];
//        [invocation setSelector:functionSelector];
//        [invocation setArgument:&indexPath.row atIndex:2];
//        [invocation invoke];
//    }
    
    
    
    switch (indexPath.row) {
        case 0:
        {
            MeHelpViewController *controller = [[MeHelpViewController  alloc]  init];
            controller.hidesBottomBarWhenPushed = YES;
            [self.childvc.navigationController pushViewController:controller animated:YES];

        }
            break;
        case 1:
        {
            MeAboutViewController *controller = [[MeAboutViewController  alloc]  init];
            controller.hidesBottomBarWhenPushed = YES;
            [self.childvc.navigationController pushViewController:controller animated:YES];

        }
            break;
        case 2:
        {
            
            UserRes *user = [CommonMethod getUser];
            if (!user) {
                [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
                return;
            }
            MeSuggestViewController *controller = [[MeSuggestViewController  alloc]  init];
            controller.hidesBottomBarWhenPushed = YES;
            [self.childvc.navigationController pushViewController:controller animated:YES];

        }
            break;
        case 3:
        {
            MeUpViewController *controller = [[MeUpViewController  alloc]  init];
            controller.hidesBottomBarWhenPushed = YES;
            [self.childvc.navigationController pushViewController:controller animated:YES];

        }
            break;

        default:
            break;
    }
}



@end
